﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Controls;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows;

namespace WpfImageDuplicates
{
    public class FileImageCollectionScrollHandler : BaseNotifyPropertyChanged
    {
        public event EventHandler ScrollChanged;
        protected ImageCollectionView _collectionView;
        private ScrollViewer _scrollViewer;
        private double _viewportWidth;
        private double _viewportHeight;
        private double _verticalOffset;
        private double _lastOffset = double.MinValue;
        public bool _needsRecalcVisible;
        //public List<ThumbnailView> _lastVisibleResults;

        public FileImageCollectionScrollHandler(ImageCollectionView collectionView)
        {
            _collectionView = collectionView;
            var sv = _collectionView.lstItems.GetAllChildren<ScrollViewer>(true);
            if (sv.Count > 0)
            {
                _scrollViewer = sv[0];
                _scrollViewer.ScrollChanged += new ScrollChangedEventHandler(_scrollViewer_ScrollChanged);
            }
            else
            {
                Console.Write("Hmm.. no children.  Investigate");
            }
        }

        void _scrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            ScrollChanged.RaiseEvent(this);
        }

        public ScrollViewer ScrollViewer
        {
            get { return _scrollViewer; }
        }

        public void ScrollPageDown()
        {
            if (ScrollViewer != null)
            {
                ScrollViewer.ScrollToVerticalOffset(Math.Min(ScrollViewer.VerticalOffset + ScrollViewer.ActualHeight, ScrollViewer.ScrollableHeight));
            }
        }

        public void ScrollBottom()
        {
            if (ScrollViewer != null)
            {
                ScrollViewer.ScrollToBottom();
            }
        }

        public void ScrollTop()
        {
            if (ScrollViewer != null)
            {
                ScrollViewer.ScrollToTop();
            }
        }

        public void Scroll(double d)
        {
            if (ScrollViewer != null)
            {
                ScrollViewer.ScrollToVerticalOffset(Math.Min(ScrollViewer.ScrollableHeight, Math.Max(ScrollViewer.VerticalOffset + d, 0)));
            }
        }

        public void ScrollPageUp()
        {
            if (ScrollViewer != null)
            {
                ScrollViewer.ScrollToVerticalOffset(Math.Max(ScrollViewer.VerticalOffset - ScrollViewer.ActualHeight, 0));
            }
        }

        public double ViewportWidth
        {
            get { return _viewportWidth; }
        }

        public double ViewportHeight
        {
            get { return _viewportHeight; }
        }

        public double VerticalOffset
        {
            get { return _verticalOffset; }
        }

        public bool UpdateVerticalOffset()
        {
            if (_scrollViewer == null)
                return false;

            try
            {
                double thisOffset = _scrollViewer.VerticalOffset;

                if (Math.Abs(_lastOffset - thisOffset) < 10D)
                    return false;

                _needsRecalcVisible = true;

                _lastOffset = thisOffset;
                RecalcScrollViewerInfo();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return true;
        }

        public void RecalcScrollViewerInfo()
        {
            try
            {
                _scrollViewer.GetScrollViewerInfo(out _viewportWidth, out _viewportHeight, out _verticalOffset);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
                        
        //public int _lastCountThumbsFromNonVis;
        //public int _lastCountThumbsFromVis;        

    }
}
